package com.nf.xiaomi.web.controller;

import com.fasterxml.jackson.core.type.TypeReference;
import com.nf.mvc.ViewResult;
import com.nf.mvc.file.MultipartFile;
import com.nf.mvc.mapping.RequestMapping;
import com.nf.mvc.support.ResponseVO;
import com.nf.mvc.util.JacksonUtils;
import com.nf.xiaomi.entity.ProductEntity;
import com.nf.xiaomi.service.ProductInfoService;
import com.nf.xiaomi.service.impl.FileService;
import com.nf.xiaomi.service.impl.ProductInfoServiceImpl;
import com.nf.xiaomi.vo.ProductInfoVO;

import java.util.List;

import static com.nf.mvc.handler.HandlerHelper.json;

@RequestMapping("/info")
public class ProductInfoController {
    ProductInfoService service=new ProductInfoServiceImpl();

    @RequestMapping("/list")
    public ViewResult getInfoList(Integer productID){
        List<ProductEntity> productInfoList = service.getProductInfoList(productID);
        return json(new ResponseVO(200, "ok", productInfoList));
    }

    @RequestMapping("/insert")
    public ViewResult addProductInfo(Integer productID,
                                     String detailList,
                                     MultipartFile[] files){
        List<ProductInfoVO> infos = JacksonUtils.fromJson(detailList,
                new TypeReference<List<ProductInfoVO>>() {});
        String[] fileNames=new String[files.length];
        for (int i = 0; i < files.length; i++) {
            String fileName = FileService.uploadFile("firstbuckets", files[i]);
            fileNames[i]=fileName;
        }

        ProductInfoService service=new ProductInfoServiceImpl();
        Integer result = service.insertProductInfo(productID, infos,fileNames );
        return json(new ResponseVO(200, "ok", 1));
    }
}
